class Solution
{
public:
    int m, n;
    int dx[4] = { 1,-1,0,0 };
    int dy[4] = { 0,0,1,-1 };
    bool vis[210][210] = { 0 };

    void dfs(vector<vector<char> >& grid, int i, int j)
    {
        vis[i][j] = true;
        for (int k = 0; k < 4; k++)
        {
            int x = i + dx[k];
            int y = j + dy[k];
            if (x < m && y < n && x >= 0 && y >= 0 && !vis[x][y] && grid[x][y] == '1')
                dfs(grid, x, y);
        }
        return;
    }
    int solve(vector<vector<char> >& grid)
    {
        int cnt = 0;
        m = grid.size();
        n = grid[0].size();
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (grid[i][j] == '1' && !vis[i][j])
                {
                    dfs(grid, i, j);
                    cnt++;
                }
            }
        }

        return cnt;
    }
};